<?php
/**
*他人个人中心控制器
*他人的关注、粉丝，收藏，资料，发布
*@author susy<susy.liang@ilongyuan.com.cn>
*/
namespace Home\Controller;

class OthersController extends HomeController {

 
    /**
    *他人的标识
    */
    private $otherId = '';

    /**
    *操作用户的标识
    */
    private $selfId = '';

    /**
    *基础参数的获取
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    protected function _initialize(){

        parent::_initialize();
        C('DEFAULT_THEME', 'ui3');

        $uid = I('get.uid', 0, 'intval');
        $user = service('Users')->detail($uid);
        if(!$user)
            $this->error('用户不存在');

        $this->otherId = $uid;
        $this->selfId = $this->uinfo['id'];

        $self = A('Api/User', 'Event')->info(UID);
        $this->assign('self',$self);
        $user = A('Api/User', 'Event')->detail($this->otherId, true);
        $this->user = $user;

        $this->isOthers = 1;

        $this->hasBGD = 1;//页面加背景图
    }

    /**
    *用户的个人资料
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */

    public function index(){
        if (UID == $this->otherId) {
            // $this->redirect('/my-videos');
            return A('Ucenter')->activity();
        }
        C('DEFAULT_THEME', 'v4');

        $videos = $this->videos();
        $this->trade = array_slice($videos, 0, 5);
        if(IS_AJAX){
            $data = [];
            foreach ($videos as $video) {
                $this->video = $video;
                $data[] = [
                    'create_month' => $video['create_month'],
                    'html' => $this->fetch('Others/video_template'),
                ];
            }
            $this->ajaxReturn(['state' => 0, 'info' => '请求成功', 'data' => $data]);
        }
        setRedirect();
        $this->bg_video = $videos ? $videos[0]['pre_uri'] : C('HOME_BG_VIDEO');
        $videos = array_group($videos, 'create_month');
        $this->videos = $videos;
        $this->total = event('video')->total(['uid' => $this->otherId]);

        $this->interest = empty($this->user['interest']) ? [] : explode(',', $this->user['interest']);
        $reward_count = event('reward')->total(['target_uid' => $this->otherId], 'target_uid');
        $like_count = service('like')->where(['target_id' => $this->otherId])->count();
        $this->statis = [
            'reward' => intval($reward_count[$this->otherId]),
            'like'   => intval($like_count),
        ];
        $this->recommend_videos = $this->recommend_videos();
        $this->assign('title', $this->user['nickname'].'的主页');
        $this->display();
    }

    private function videos() {
        $this->p = I('p', 1, 'intval');
        $this->pager = I('pager', 12, 'intval');

        $map = [
            'uid' => $this->otherId,
        ];
        $fields = 'id,uid,title,pre_uri,thumb,play,time,create_time,comments,play';
        $videos = event('Video')->getBaseInfoLists($map, true, $fields, $this->p, $this->pager);
        foreach ($videos as &$video) {
            $video['create_month'] = date('Y-m', $video['create_time']);
        }
        return $videos;
    }

    private function recommend_videos()
    {
        $vids = event('video')->randomLists(['uid' => ['$ne' => $this->otherId]], 6);
        $fields = 'id,uid,title,pre_uri,thumb,play,time,create_time,comments,play';
        $videos = event('Video')->getBaseInfoLists(['id' => ['in', $vids?:[]]], true, $fields);
        return $videos;
    }

    public function follow_users()
    {
        $limit = I('limit', 5, 'intval');
        $users = event('follow')->follows($this->otherId, true, [], $limit);
        $this->ajaxReturn(['state' => 0, 'data' => $users ?: []]);
    }
    /**
    *用户的动态
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    //modify by liubo <liubo@ilongyuan.com.cn> at 2016-04-18
    private function trade($timestamp, $pager){

        $servVideo = D('Common/Video', 'Service');

        $condition['status'] = $servVideo::STATUS_PASS;
        $condition['uid'] = $this->otherId;
        $condition['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
        $res = $servVideo->where($condition)->order('create_time desc')->limit($pager)->field('id')->select();

        $videos = [];
        foreach ($res as $key => $value) {
            $videos[] = $value['id'];
        }
        $filterMap = array('id','title','thumb','pre_uri','uri', 'dpi_levels', 'detail','user','statis','action','create_time','game_id','game','type','icons', 'tags', 'type', 'status', 'dpi_height', 'dpi_width');
        $videos = $this->lists_format($videos, $filterMap);

        if($videos){
            $comEvent  = A('Api/Comments', 'Event');
			$comments = array();
			foreach ($videos as $key => &$video) {
				$video['comments'] = $comEvent->get($video['id'], $this->commentPager, '', array(), true);
                $video['comments'] = array_map(function($comment){
                    $comment['content'] = htmlspecialchars($comment['content']);
                    return $comment;
                }, $video['comments']);
				$video['comments_show_count'] = count($video['comments']);
			}
		}
        return $videos;
    }

    /**
     * 动态数量
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function tradeCount() {
        $servVideo = D('Common/Video', 'Service');

        // $condition['status'] = $servVideo::STATUS_PASS;
        $condition['uid'] = $this->otherId;
        //动态数量
        $count = $servVideo->where($condition)->count();
        return $count;
    }

    /**
    *判断是否小编用户
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    private function isEditer(){

        $homeGrp = M('auth_group')->where(array('module'=>'home'))->getField('id', true);
        if($homeGrp){
            $user = M('auth_group_access')->where(array('group_id'=>array('in', $homeGrp)))->getField('uid', true);
            if(in_array($this->otherId, $user))
                return true;
        }

        return false;
    }

    /**
    *用户的粉丝
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    public function fans(){

        $data = array();

        $fevtObj = A('Api/Follow', 'Event');
        $total = $fevtObj->count_follows($this->otherId);
        if($total){
            
            $pager = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');
            $pageObj = new \Think\Page($total, $pager);
            if($total>$pager){
                $pageObj->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            }
            $page =$pageObj->show();
            $this->assign('page', $page? $page: '');
            $this->assign('total',$total);

            $p = I('p', 1, 'intval');
            $fans = $fevtObj->follows($this->otherId, false, array(), $pager, $p);

            $uEvent = A('Api/User','Event');
            $field = 'uid as id, nickname, avatar';
            foreach ($fans as $key => $value) {
                $user = $uEvent->detail($value, false, $field);
                $user['is_follow'] = intval($fevtObj->is_follow($this->selfId, $value));
                $data[] = $user;
            }
        }

        $this->data = $data;

        $this->user = A('Api/User', 'Event')->detail($this->otherId, true);
        $this->hotVideo = A('Home/Video')->recommend();

        $this->display();
        //$this->success('操作成功', $data);

    }

    /**
    *用户的好友就，即用户的关注
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    public function friend(){
        $data = array();

        $fevtObj = A('Api/Follow', 'Event');
        $total = $fevtObj->count_mine($this->otherId);
        if($total){

            $limitNum = C('FANS_CENTER_NUM');
            $pageObj = new \Think\Page($total, $limitNum);
            if($total>$limitNum){
                $pageObj->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            }

            $page =$pageObj->show();
            $this->assign('page', $page? $page: '');
            $this->assign('total',$total);

            $p = I('p', 1, 'intval');
            $pager     = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');
            $mine = $fevtObj->mine($this->otherId, false, array(), $pager, $p);

            $uEvent = A('Api/User','Event');
            $field = 'uid as id, nickname, avatar';
            foreach ($mine as $key => $value) {
                $user = $uEvent->detail($value, false, $field);
                $user['is_follow'] = intval($fevtObj->is_follow($this->selfId, $value));
                $data[] = $user;
            }
        }

        $this->data = $data;

        $this->user = A('Api/User', 'Event')->detail($this->otherId, true);
        $this->hotVideo = A('Home/Video')->recommend();

        $this->display();
        
    }

    /**
    *用户订阅的游戏
    *@author susy.liang <liangling@ilongyuan.com.cn>
    */
    public function game(){

        $data = array();

        $gsevtObj = A('Api/GameScribe', 'Event');
        $total = $gsevtObj->count_mine($this->otherId);
        if($total){

            $limitNum = C('GAMES_CENTER_NUM');
            $pageObj = new \Think\Page($total, $limitNum);
            if($total>$limitNum){
                $pageObj->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            }
            $page =$pageObj->show();
            $this->assign('page', $page? $page: '');
            $this->assign('total',$total);

            $games = $gsevtObj->mine($this->otherId, true, $p, $limitNum);
            foreach ($games as $key => $value) {
                uri_format($value['pic']);
                uri_format($value['cover']);

                if($this->selfId)
                    $value['is_scribe'] = intval($gsevtObj->is_scribe($this->selfId, $value['id']));
                else
                    $value['is_scribe'] = 0;
                
                array_push($data, $value);
            }
        }
        $this->data = $data;

        $this->user = A('Api/User', 'Event')->detail($this->otherId, true);
        $this->hotVideo = A('Home/Video')->recommend();

        $this->display();
        //$this->success('操作成功', $data);
    }
}